<template>
    <view>
        <custom><template #backText>个人资料</template></custom>
        <view>
            <view class="bg-white flex justify-center text-center padding-tb-30">
                <view @tap="chooseAvatar">
                    <avatar :avatar_url="nicknameAvatar.form.avatar" avatar_size="xl"></avatar>
                </view>
            </view>
            <nFormItem title="昵称" text_lr="right" :data.sync="nicknameAvatar" field="nickname"></nFormItem>
            <nFormItem type="switch" title="是否匿名" text_lr="right" :data.sync="nicknameAvatar" field="is_anonym"></nFormItem>
        </view>
        <view class="margin-top-30">
            <nFormItem title="真实姓名" text_lr="right" :data.sync="truename" field="truename" :disabled="!!truename.formbak.truename"></nFormItem>
            <nFormItem title="身份证号" format="number" text_lr="right" :data.sync="truename" field="idcard" :disabled="!!truename.formbak.idcard"></nFormItem>
            <nFormItem title="支付宝号" text_lr="right" :data.sync="truename" field="alipay_account" :disabled="!!truename.formbak.alipay_account"></nFormItem>
            <formProvinceCityArea title="所在地区" text_lr="right" :data.sync="truename" :disabled="truename.formbak.area_code != 0"></formProvinceCityArea>
        </view>
        <nFormButton v-if="!objectIsEq(nicknameAvatar.form, nicknameAvatar.formbak) || !objectIsEq(truename.form, truename.formbak)"
            title="保存资料" :tap="save"></nFormButton>
    </view>
</template>

<script>
    import formProvinceCityArea from '@/components/nddyny/form/provinceCityArea.vue';
    export default {
        components: {
            formProvinceCityArea
        },
        data() {
            const identity_info = this.$store.state.user.identity_info;
            const nicknameAvatarForm = {
                avatar: identity_info.avatar,
                nickname: identity_info.nickname,
                is_anonym: identity_info.is_anonym
            };
            const truenameForm = {
                truename: identity_info.truename,
                idcard: identity_info.idcard,
                alipay_account: identity_info.alipay_account,
                area_code: identity_info.area_code
            };
            return {
                nicknameAvatar: {
                    formtag: {},
                    form: this.$nddyny.object.copy(nicknameAvatarForm),
                    formbak: this.$nddyny.object.copy(nicknameAvatarForm),
                },
                truename: {
                    formtag: {},
                    form: this.$nddyny.object.copy(truenameForm),
                    formbak: this.$nddyny.object.copy(truenameForm),
                }
            }
        },
        methods: {
            save() {
                if (!this.$nddyny.object.isEq(this.nicknameAvatar.form, this.nicknameAvatar.formbak)) {
                    this.saveNicknameAvatar();
                } else if (!this.$nddyny.object.isEq(this.truename.form, this.truename.formbak)) {
                    this.saveTruenameForm();
                }
            },
            saveNicknameAvatar() {
                if (this.nicknameAvatar.form.avatar == this.nicknameAvatar.formbak.avatar) {
                    this.trueSaveNicknameAvatar();
                    return;
                }
                this.$api.upload(this, {
                    loading: '上传头像中',
                    url: '/account/info/uploadAvatar',
                    filePath: this.nicknameAvatar.form.avatar,
                    success: (Result) => {
                        if (this.$api.form.toast(this, Result, 'nicknameAvatar')) return;
                        this.nicknameAvatar.form.avatar = Result.result['oss-request-url'];
                        this.trueSaveNicknameAvatar();
                    }
                });
            },
            trueSaveNicknameAvatar() {
                this.$api.post(this, {
                    loading: '保存个人资料中',
                    url: '/account/info/updateNicknameAvatarAnonym',
                    data_key: 'nicknameAvatar',
                    success: (Result, data_key) => {
                        if (this.$api.form.toast(this, Result, data_key)) return;
                        this.nicknameAvatar.formbak = this.$nddyny.object.copy(this.nicknameAvatar.form);
                        this.$user.updateIdentityInfo(this);
                        if (!this.$nddyny.object.isEq(this.truename.form, this.truename.formbak)) {
                            this.saveTruenameForm();
                        }
                        this.$store.commit('userToastSuccess', '保存成功');
                    }
                });
            },
            saveTruenameForm() {
                if (this.$store.state.user.is_realname == 1) return;
                this.$api.post(this, {
                    loading: '实名认证中',
                    url: '/user/info/realnameVerify',
                    data_key: 'truename',
                    success: (Result, data_key) => {
                        if (this.$api.form.toast(this, Result, data_key)) return;
                        this.truename.formbak = this.$nddyny.object.copy(this.truename.form);
                        this.$user.updateIdentityInfo(this);
                        this.$store.commit('userToastSuccess', '认证成功');
                    }
                });
            },
            chooseAvatar() {
                uni.chooseImage({
                    count: 1,
                    sizeType: ['compressed'],
                    sourceType: ['album', 'camera'],
                    success: (res) => {
                        this.nicknameAvatar.form.avatar = res.tempFiles[0].path;
                    }
                });
            },
            objectIsEq(obj1, obj2) {
                return this.$nddyny.object.isEq(obj1, obj2);
            },
        },
    }
</script>
